<?php
/**
 * 控制器基类
 * 
 * @author zhoujing
 */
class Control extends Base
{
	private $_cache = false;			//是否开启静态缓存
	private $_cache_time = 86400;		//缓存 默认时间
	
	public function __construct(){}
	
	/**
	 * 视图输出处理
	 * 
	 * @param array $data 传递到视图中的数据
	 * @param boolean $out 是否输出视图，默认输出
	 */
	protected function view($data = array(), $output = true)
	{
		//首先组合出视图文件路径
		$pre = APP.'/view';
		if(Router::$_dir !== false)
		{
			$pre .= '/'.Router::$_dir;
		}
		$dir = $pre.'/'.Router::$_control;
		$path = $dir.'/'.Router::$_action.'.php';
		
		//传递变量到视图文件
		foreach($data as $key => $value)
		{
			$$key = $value;
		}
		
		ob_start();
		require($path);
		
		//如果开启了缓存的话要静态化
		if($this->_cache === true && $output === true)
		{
			$cache = ob_get_contents();
			$cache_file = $dir.'/~'.Router::$_action.'.php';
			$expire = time()+$this->_cache_time;
			$fp = fopen($cache_file, 'w+');
			if (flock($fp, LOCK_EX))
			{
				fwrite($fp, $expire.'~~~>'.$cache);
				flock($fp, LOCK_UN);
				fclose($fp);
			}
			unset($cache);
		}
		
		$res = ob_get_contents();
		if($output === false)
		{
			ob_end_clean();
			return $res;
		}
		ob_end_flush();
	}
	
	/**
	 * 开启缓存并且设置缓存过期时间
	 * 
	 * @param int $expire 秒
	 */
	protected function cache($expire = 0)
	{
		$this->_cache = true;
		if($expire !== 0)
		{
			$this->_cache_time = $expire;
		}

		//首先组合出视图文件路径
		$pre = APP.'/view';
		if(Router::$_dir !== false)
		{
			$pre .= '/'.Router::$_dir;
		}
		$dir = $pre.'/'.Router::$_control;
		$path = $dir.'/~'.Router::$_action.'.php';
		
		//检测是否有缓存文件存在
		if(file_exists($path))
		{
			//缓存文件存在继续判断缓存是否过期
			$fp = fopen($path, 'r');
			flock($fp, LOCK_SH);
			$cache = fread($fp, filesize($path));
			flock($fp, LOCK_UN);
			fclose($fp);
			
			//缓存时间检索出来并且没有过期，直接输出缓存
			if(preg_match('/(\d+~~~>)/', $cache, $match))
			{
				if(time() < (int)trim(str_replace('~~~>', '', $match[1])))
				{
					$times = 1;
					echo str_replace($match[1], '', $cache, $times);
					exit ;
				}
			}
		}
	}
	
	/**
	 * model加载方法。支持dir/name的嵌套目录形式调用。
	 * 若model和control同名，则model类名前加下划线“_”
	 * 
	 * @param unknown_type $name
	 */
	protected function model($name = null)
	{

		if($name === null)
		{
			$name = '_'.Router::$_control;
		}
		
		if(isset($this->$name))
		{
			return $this->$name;
		}
		
		$model_name = $name;
		$name = trim(str_replace('\\', '/', $name), '/');
		if(strpos($name, '/') !== false)
		{
			$name_arr = explode('/', $name);
			$model_name = array_pop($name_arr);
		}
		
		__Gvar::$loader->file(APP.'/model/'.$name.'.php');
		
		return $this->$model_name = new $model_name();
		
	}
	
	/**
	 * 生成静态文件URL，带版本号,只针对img，js，css
	 *
	 * @param string $name 静态文件名，可以包含路径信息
	 * @return string 组装好的静态文件路径信息
	 */
	protected function vlink($name)
	{
		$name = str_replace('\\', '/', trim($name, '\\/'));
		$pattern = "/.*\.(jpeg|jpg|png|gif|js|css)/i";
		if(!preg_match($pattern, $name, $match))
		{
			return $name;
		}
	
		$suf = strtolower($match[1]);
		if($suf == 'jpeg' || $suf == 'jpg' || $suf == 'png' || $suf == 'gif')
		{
			$type = $this->item('images');
		}
		if($suf == 'js')
		{
			$type = $this->item('scripts');
		}
		if($suf == 'css')
		{
			$type = $this->item('styles');
		}
	
		$type = str_replace('\\', '/', trim($type, '/'));
	
		return $this->item('static_url').'/'.$type.'/'.$name.'?v='.$this->item('version');
	}
}